import numpy as np
import matplotlib.pyplot as plt
def funtion_1(x):
    return 0.01*x**2 + 0.1*x
# 计算中心差分
def numerical_diff(f,x):
    h = 1e-4
    return (f(x+h) - f(x-h))/(2*h)

# 返回切线函数
def tangent_line(f,x):
    d = numerical_diff(f,x)
    print(d)
    y = f(x) - d*x
    return lambda t: d*t + y

if __name__ == '__main__':
    x = np.arange(0.0, 20.0, 0.1)
    y = funtion_1(x)
    plt.xlabel("x")
    plt.ylabel("f(x)")
    tf = tangent_line(funtion_1,5)
    y2 = tf(x)
    plt.plot(x, y)
    plt.plot(x, y2)
    plt.show()